matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
def rotate(matrix):
    n=len(matrix)   #求解matrix的维度大小
    #左边界，右边界
    leftPointer,rightPointer=0,n-1
    while leftPointer < rightPointer:
        tempUp = matrix[leftPointer][leftPointer:rightPointer + 1]
        tempRight=[]
        for i in range(leftPointer,rightPointer+1):
            tempRight.append(matrix[i][rightPointer])
        tempDown=matrix[rightPointer][leftPointer:rightPointer+1]
        tempLeft=[]
        for i in range(leftPointer,rightPointer+1):
            tempLeft.append(matrix[i][leftPointer])
        print(tempUp)
        print(tempRight)
        print(tempDown)
        print(tempLeft)
        for i in range(leftPointer,rightPointer+1):
            matrix[leftPointer][rightPointer-i+leftPointer]=tempLeft[i-leftPointer]
        for i in range(leftPointer,rightPointer+1):
            matrix[i][rightPointer]=tempUp[i-leftPointer]
        for i in range(leftPointer,rightPointer+1):
            matrix[rightPointer][rightPointer-i+leftPointer]=tempRight[i-leftPointer]
        for i in range(leftPointer,rightPointer+1):
            matrix[i][leftPointer]=tempDown[i-leftPointer]
        leftPointer+=1
        rightPointer-=1
    print(matrix)
rotate(matrix)
